﻿// <copyright file="HandleFormPasswordBox.cs" company="Microsoft Corporation">
// Copyright (c) 2009 Microsoft Corporation All Rights Reserved
// </copyright>
// <author>Michael S. Scherotter</author>
// <email>mischero@microsoft.com</email>
// <date>2010-01-29</date>
// <summary>HandleFormPasswordBox class definition</summary>

namespace Microsoft.WebAnalytics.Behaviors.FormTrackers
{
    using System;
    using System.Windows.Controls;
    using Microsoft.WebAnalytics.Contracts;

    /// <summary>
    /// Handle the PasswordBox.PasswordChanged event for a form control
    /// </summary>
    [FormTracker(typeof(PasswordBox))]
    public class HandleFormPasswordBox : ITrackFormControl
    {
        #region Fields
        /// <summary>
        /// The event handler to track against
        /// </summary>
        private EventHandler<FormEventArgs> handler;
        #endregion

        #region ITrackFormControl Members

        /// <summary>
        /// Attach to the PasswordBox.PasswordChanged event
        /// </summary>
        /// <param name="control">a CheckBox</param>
        /// <param name="eventHandler">the Event handler to dispatch to</param>
        public void AttachEvents(Control control, EventHandler<FormEventArgs> eventHandler)
        {
            System.Diagnostics.Debug.Assert(control != null, "control cannot be null.");

            System.Diagnostics.Debug.Assert(eventHandler != null, "handler cannot be null.");

            this.handler = eventHandler;

            var passwordBox = control as PasswordBox;

            passwordBox.PasswordChanged += new System.Windows.RoutedEventHandler(this.PasswordChanged);
        }

        /// <summary>
        /// PasswordChanged event handler
        /// </summary>
        /// <param name="sender">the PasswordBox</param>
        /// <param name="e">the routed event arguments</param>
        private void PasswordChanged(object sender, System.Windows.RoutedEventArgs e)
        {
            var formEvent = new FormEventArgs
            {
                Control = sender as Control,
                EventName = "PasswordChanged",
                InnerEvent = e
            };

            this.handler(this, formEvent);
        }

        #endregion
    }
}